package cn.kgc.crowdfundingadmin2.controller;

import cn.kgc.crowdfundingadmin2.common.CrowdFundingConstant;
import cn.kgc.crowdfundingadmin2.common.ResultEntity;
import cn.kgc.crowdfundingadmin2.pojo.Admin;
import cn.kgc.crowdfundingadmin2.service.AdminService;
import com.github.pagehelper.PageInfo;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class AdminController {

    @Resource
    AdminService adminService;

    @RequestMapping("/admin/do/login")
    public String doLogin(String loginAcct, String userPswd, HttpSession session,Model model){
        Admin admin = adminService.login(loginAcct, userPswd);
        if (admin!=null){
            session.setAttribute(CrowdFundingConstant.ATTR_NAME_LOGIN_ADMIN,admin);
//            return "admin-main";
            return "redirect:/admin/to/main/page.html";
        }else{
            //登录失败
            model.addAttribute(CrowdFundingConstant.ATTR_NAME_MESSAGE,CrowdFundingConstant.MESSAGE_LOGIN_FAILED);
            return "admin-login";
        }
    }

    //分页查询用户
    @RequestMapping("/admin/query/for/search")
    public String queryForSearch(@RequestParam(value = "pageNum",required = false,defaultValue = "1") Integer pageNum,
                                 @RequestParam(value="pageSize", defaultValue="5",required = false) Integer pageSize,
                                 @RequestParam(value="keyword", defaultValue="",required = false) String keyword,
                                 Model model){
        PageInfo<Admin> pageInfo = adminService.queryForKeywordSearch(pageNum, pageSize, keyword);
        model.addAttribute(CrowdFundingConstant.ATTR_NAME_PAGE_INFO, pageInfo);
        System.out.println(pageInfo.toString());
        return "admin-page";
    }

    //删除
    @RequestMapping("/admin/batch/remove")
    @ResponseBody
    public ResultEntity<String> batchRemove(@RequestBody List<Integer> adminIdList){
        adminService.remove(adminIdList);
        return ResultEntity.successWithoutData();//{result:SUCCESS,message:NO_MESSAGE,data:NO_DATA}
    }

    //退出
    @RequestMapping("/admin/logout")
    public String logout(HttpSession session){
        session.invalidate();
        return "redirect:/index";
    }

    //局部异常处理
    @ExceptionHandler(value=Exception.class)
    public String catchException(Exception exception,Model model) {
        model.addAttribute(CrowdFundingConstant.ATTR_NAME_ERROR_MESSAGE, exception);
        return "system-error";
    }

    //添加
    @RequestMapping("/admin/save")
    public String save(Admin admin){
        adminService.saveAdmin(admin);
        return "redirect:/admin/query/for/search";
    }

    //打开修改页面
    @RequestMapping("/admin/to/edit/page")
    public String toEditPage(Integer adminId, Integer pageNum, Model model){
        //根据id查询Admin
        Admin admin=adminService.selectById(adminId);
        //保存数据
        model.addAttribute(CrowdFundingConstant.ATTR_ADMIN,admin);
        //跳转页面
        return "admin-edit";
    }

    //修改
    @RequestMapping("/admin/edit")
    public String edit(Integer pageNum,Admin admin){
        //修改用户
        adminService.updateAdmin(admin);
        //跳转页面，保持当前页
        return "redirect:/admin/query/for/search?pageNum="+pageNum;
    }
}
